<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.btn"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-del>
            <el-button :disabled="!canDelete" @click="del()">删除</el-button>
          </template>
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :tab-index="false"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :pager="false"
        :spanMethod="spanMethod"
        @handleEvent="handleEvent"
      />
    </div>
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      @returnModify="getList"
    />
  </div>
</template>

<script setup name="cmVar">
import Modify from './modify'
import { getProductVarieties, delProductVarietiesNames } from '@/api/cmPreMgt'

const { proxy } = getCurrentInstance()

// 过滤相关配置
const filterInfo = ref({
  query: {},
  copyQuery: {},
  list: [
    {
      componentType: 'input',
      label: '品种名称',
      value: 'varietiesNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '厂家名称',
      value: 'brandNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '产品编码',
      value: 'productCodeLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '产品名称',
      value: 'commonNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '规格',
      value: 'standardLike',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('cmVar:add'),
    },
    {
      type: 'slot',
      value: 'del',
      show: proxy.$permission('cmVar:del'),
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getProductVarieties,
  data: [],
  selections: [],
  fieldList: [
    { label: '序号', value: 'index', width: 40 },
    { label: '品种名称', value: 'varietiesName' },
    { label: '厂家', value: 'brandName' },
    { label: '产品编码', value: 'productCode' },
    { label: '产品名称', value: 'commonName' },
    { label: '规格', value: 'standard' },
    { label: '计量单位', value: 'unitName' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '60',
    btList: [
      {
        label: '删除',
        event: del,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('cmVar:del'),
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
})
const canDelete = computed(() => {
  return tableInfo.value.selections.length
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function spanMethod({ row, column, rowIndex, columnIndex }) {
  const afterIndexs = [8, 9, 10]
  let keys = new Array(11)
  keys[2] = 'varietiesName'
  if (afterIndexs.includes(columnIndex)) {
    keys[columnIndex] = 'index'
  } else {
    keys = ['index', 'index', 'varietiesName', 'brandName', 'productCode']
  }
  // 注意:字段列表要按表格排列的顺序排列,如果后续字段不再需要进行行合并,可以省略后面的字段,如果中间某字段要跳过合并,要用空字符串顶位
  return proxy.$fn.spanMethod(tableInfo.value.data, keys, rowIndex, columnIndex)
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//创建
function create() {
  modifyInfo.value.title = '新增品种试剂'
  modifyInfo.value.visible = true
}
//删除
function del(row = undefined) {
  const ids = _getIds(row)
  delProductVarietiesNames(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function _getIds(row) {
  let varietiesIds = []
  if (row && row.varietiesId) {
    varietiesIds = [row.varietiesId]
  } else {
    if (tableInfo.value.selections.length > 0) {
      tableInfo.value.selections.forEach((row) => {
        varietiesIds.push(row.varietiesId)
      })
    }
  }
  varietiesIds = [...new Set(varietiesIds)]
  return varietiesIds
}
</script>

<style scoped></style>
